def p (n):
    f=True
    for i in range (2,n):
        if n % i==0:
            f = False
            break
    return f
x = int(input())
if 999 < x < 10000:
    a = 0
    if p(x):
        a = x
    else:
        if p(x//10):
            a = x//10
        if p(x%1000):
            a = max (a, x % 1000)
        else:
            if p(x//100):
                a = max(a, x//100)
            if p(x%100):
                a=max(a, x%100)
            if p(x% 1000 // 10):
                a = max(a, x %1000 // 10)
            else:
                if p(x // 1000):
                    a = max (a, x//1000)
                if p(x // 100 % 10):  
                    a = max (a, x//100%10)
                if p(x // 100 //10):
                    a = max ( a, x // 100 //10)
                if p(x % 10):
                    a = max (a, x % 10)
    print (a)
else:
    print (0)                
                
                 
                 
            